Set Up

rm(list = ls())
set.seed(2024)
library(tidyverse)
library(here)
library(phyloseq)
library(vegan)
library(rstatix)
theme_set(theme_bw())
max.core <- parallel::detectCores()

ps.rare <- readRDS(here('data','following_study','ps_rarefied.rds')) 
sample_data(ps.rare)$Shannon <- estimate_richness(ps.rare)$Shannon
# transform data into proportion
ps.rare.prop <- ps.rare %>% transform_sample_counts(function(x) x/sum(x))

sam <- data.frame(sample_data(ps.rare))

Functions

plot_ord draws ordination plot for different factors using plot_ordination function in phyloseq package.

permanova performs permutational multivariate analysis of variance (PERMANOVA) based on adonis2 function in vegan package.

permdisp performs permutational analysis of multivariate dispersions (PERMDISP) based on betadisper function in vegan package.

plot_ord <- function(data, factor, method, distance){
    data.ord <- ordinate(data, method = method, distance = distance)
    p <- plot_ordination(data, data.ord, color = factor)
    p <- p + stat_ellipse(type = "t",geom = "polygon",alpha = 0)
    p <- p + ggtitle(str_c(factor,method,distance, sep = ' - '))
    print(p)
}
permanova <- function(data, formula, method, permutations=1e4, strata = NULL, core = max.core){
    message('PERMANOVA Model: ', method, '~', formula, '; Strata: ', ifelse(is_null(strata), 'None', as.character(strata)))
    dist.matrix <- phyloseq::distance(data, method=method)
    df <- data.frame(sample_data(data))
    model <- as.formula(paste0('dist.matrix~', formula))
    if (!is_null(strata)) {strata <- df[,strata]}
    result <- adonis2(model,
                      data = df,
                      permutations=permutations,
                      strata = strata,
                      parallel = core,
                      by = 'term',
                      na.action = na.omit)
    return(result)
}
permdisp <- function(data, group, method, permutations=1e4, pairwise = FALSE, core = max.core){
    message('PERMDISP Model: ', method, '~', group)
    dist.matrix <- phyloseq::distance(data, method=method)
    df <- data.frame(sample_data(data))
    beta.disp <- betadisper(dist.matrix, group = df[,group])
    result <- permutest(beta.disp, permutations = permutations, pairwise = pairwise, type = 'centroid')
    return(result)
}

Start

In this section, we want to estimate the effect of different factors on the microbial diversity. The factors we are focusing on are Household, Epileptic.or.Control, Breed.Group..1., Pheno.Y.N, Sex, and Age..months.. We compare the species richness (Shannon index) among different factors using ANOVA, and compare the centroid of dissimilarity of microbial community between different groups using PERMANOVA using the Bray-Curtis and weighted Unifrac distance, and visualized using multi-dimensional scaling. PERMDISP was used to test the homogeneity of multivariate dispersions among groups.

Household Effect

Alpha Diversity

ggplot(sam,aes(x = as.numeric(Household), y = Shannon, group = Household)) +
    geom_point() + geom_line() + xlab('Household')

anova_test(Shannon~Household, data = sam, type = 1)
## ANOVA Table (type I tests)
## 
##      Effect DFn DFd     F     p p<.05  ges
## 1 Household  48  49 1.599 0.053       0.61

Here we see the Shannon diversity index is significantly different among households.

Beta Diversity

Bray-Curtis distance

permanova(ps.rare.prop, 'Household', 'bray')
## PERMANOVA Model: bray~Household; Strata: None
## Permutation test for adonis under reduced model
## Terms added sequentially (first to last)
## Permutation: free
## Number of permutations: 10000
## 
## adonis2(formula = model, data = df, permutations = permutations, by = "term", parallel = core, na.action = na.omit, strata = strata)
##           Df SumOfSqs     R2      F    Pr(>F)    
## Household 48  12.2248 0.6893 2.2647 9.999e-05 ***
## Residual  49   5.5104 0.3107                     
## Total     97  17.7352 1.0000                     
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

Weighted-Unifrac distance

permanova(ps.rare.prop, 'Household', 'wunifrac')
## PERMANOVA Model: wunifrac~Household; Strata: None
## Permutation test for adonis under reduced model
## Terms added sequentially (first to last)
## Permutation: free
## Number of permutations: 10000
## 
## adonis2(formula = model, data = df, permutations = permutations, by = "term", parallel = core, na.action = na.omit, strata = strata)
##           Df SumOfSqs      R2      F    Pr(>F)    
## Household 48  1.90306 0.67767 2.1462 9.999e-05 ***
## Residual  49  0.90517 0.32233                     
## Total     97  2.80823 1.00000                     
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

Epileptic Effect

Alpha Diversity

ggplot(sam, aes(x = Epileptic.or.Control, y = Shannon)) + 
    geom_boxplot() + geom_jitter(height = 0, width = 0.25)

anova_test(Shannon~Household+Epileptic.or.Control, data = sam, type = 1)
## ANOVA Table (type I tests)
## 
##                 Effect DFn DFd     F     p p<.05   ges
## 1            Household  48  48 1.568 0.061       0.611
## 2 Epileptic.or.Control   1  48 0.067 0.796       0.001

Beta Diversity

Bray-Curtis distance

plot_ord(ps.rare.prop, 'Epileptic.or.Control','MDS','bray')

plot_ord(ps.rare.prop, 'Epileptic.or.Control','NMDS','bray')
## Run 0 stress 0.2089049 
## Run 1 stress 0.2200629 
## Run 2 stress 0.2215701 
## Run 3 stress 0.2191062 
## Run 4 stress 0.2105267 
## Run 5 stress 0.233624 
## Run 6 stress 0.2301806 
## Run 7 stress 0.2046962 
## ... New best solution
## ... Procrustes: rmse 0.02758093  max resid 0.2550759 
## Run 8 stress 0.2225389 
## Run 9 stress 0.2040715 
## ... New best solution
## ... Procrustes: rmse 0.03769366  max resid 0.3007093 
## Run 10 stress 0.2213932 
## Run 11 stress 0.2217022 
## Run 12 stress 0.2115462 
## Run 13 stress 0.2106959 
## Run 14 stress 0.2097634 
## Run 15 stress 0.2042154 
## ... Procrustes: rmse 0.03712412  max resid 0.2768694 
## Run 16 stress 0.2087932 
## Run 17 stress 0.2212572 
## Run 18 stress 0.2053057 
## Run 19 stress 0.204233 
## ... Procrustes: rmse 0.03415718  max resid 0.3155705 
## Run 20 stress 0.2206824 
## *** Best solution was not repeated -- monoMDS stopping criteria:
##      5: no. of iterations >= maxit
##     15: stress ratio > sratmax

permanova(ps.rare.prop, 'Epileptic.or.Control', 'bray', strata = 'Household')
## PERMANOVA Model: bray~Epileptic.or.Control; Strata: Household
## Permutation test for adonis under reduced model
## Terms added sequentially (first to last)
## Blocks:  strata 
## Permutation: free
## Number of permutations: 10000
## 
## adonis2(formula = model, data = df, permutations = permutations, by = "term", parallel = core, na.action = na.omit, strata = strata)
##                      Df SumOfSqs      R2      F  Pr(>F)  
## Epileptic.or.Control  1   0.1584 0.00893 0.8651 0.09049 .
## Residual             96  17.5768 0.99107                 
## Total                97  17.7352 1.00000                 
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
permdisp(ps.rare.prop, 'Epileptic.or.Control', 'bray')
## PERMDISP Model: bray~Epileptic.or.Control
## 
## Permutation test for homogeneity of multivariate dispersions
## Permutation: free
## Number of permutations: 10000
## 
## Response: Distances
##           Df  Sum Sq   Mean Sq      F N.Perm Pr(>F)
## Groups     1 0.00182 0.0018249 0.1221  10000 0.7335
## Residuals 96 1.43422 0.0149398

Weighted-Unifrac distance

plot_ord(ps.rare.prop, 'Epileptic.or.Control','MDS','wunifrac')

plot_ord(ps.rare.prop, 'Epileptic.or.Control','NMDS','wunifrac')
## Run 0 stress 0.1677058 
## Run 1 stress 0.1773885 
## Run 2 stress 0.1662523 
## ... New best solution
## ... Procrustes: rmse 0.04596998  max resid 0.1482302 
## Run 3 stress 0.167717 
## Run 4 stress 0.1658647 
## ... New best solution
## ... Procrustes: rmse 0.0622066  max resid 0.358157 
## Run 5 stress 0.1708145 
## Run 6 stress 0.1699045 
## Run 7 stress 0.1703389 
## Run 8 stress 0.1638635 
## ... New best solution
## ... Procrustes: rmse 0.01674709  max resid 0.1152441 
## Run 9 stress 0.1765167 
## Run 10 stress 0.171215 
## Run 11 stress 0.1739947 
## Run 12 stress 0.1712411 
## Run 13 stress 0.1734899 
## Run 14 stress 0.171567 
## Run 15 stress 0.1691978 
## Run 16 stress 0.1660252 
## Run 17 stress 0.1706175 
## Run 18 stress 0.1727989 
## Run 19 stress 0.1740083 
## Run 20 stress 0.1685239 
## *** Best solution was not repeated -- monoMDS stopping criteria:
##      2: no. of iterations >= maxit
##     18: stress ratio > sratmax

permanova(ps.rare.prop, 'Epileptic.or.Control', 'wunifrac', strata = 'Household')
## PERMANOVA Model: wunifrac~Epileptic.or.Control; Strata: Household
## Permutation test for adonis under reduced model
## Terms added sequentially (first to last)
## Blocks:  strata 
## Permutation: free
## Number of permutations: 10000
## 
## adonis2(formula = model, data = df, permutations = permutations, by = "term", parallel = core, na.action = na.omit, strata = strata)
##                      Df SumOfSqs      R2      F Pr(>F)  
## Epileptic.or.Control  1  0.04037 0.01437 1.4001 0.0423 *
## Residual             96  2.76786 0.98563                
## Total                97  2.80823 1.00000                
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
permdisp(ps.rare.prop, 'Epileptic.or.Control', 'wunifrac')
## PERMDISP Model: wunifrac~Epileptic.or.Control
## 
## Permutation test for homogeneity of multivariate dispersions
## Permutation: free
## Number of permutations: 10000
## 
## Response: Distances
##           Df  Sum Sq   Mean Sq     F N.Perm Pr(>F)
## Groups     1 0.00698 0.0069804 1.867  10000 0.1746
## Residuals 96 0.35893 0.0037389

Breed Effect

Alpha Diversity

sam.breed <- sam %>% filter(is.na(Breed.Group..1.) == FALSE)
ggplot(sam.breed) +
    geom_point(aes(x = Breed.Group..1., y = Shannon, colour = Breed.Group..1.)) +
    facet_wrap(~Epileptic.or.Control) + 
    theme(axis.text.x = element_blank(), axis.ticks.x.bottom = element_blank())

anova_test(Shannon~Household + Breed.Group..1., data = sam.breed, type = 1)
## ANOVA Table (type I tests)
## 
##            Effect DFn DFd     F     p p<.05   ges
## 1       Household  45  39 1.572 0.076       0.645
## 2 Breed.Group..1.   4  39 2.892 0.034     * 0.229

Beta Diversity

Bray-Curtis distance

plot_ord(ps.rare.prop, 'Breed.Group..1.','MDS','bray')
## Too few points to calculate an ellipse
## Too few points to calculate an ellipse

plot_ord(ps.rare.prop, 'Breed.Group..1.','NMDS','bray')
## Run 0 stress 0.2089049 
## Run 1 stress 0.2158377 
## Run 2 stress 0.2283251 
## Run 3 stress 0.2365669 
## Run 4 stress 0.2030056 
## ... New best solution
## ... Procrustes: rmse 0.04265531  max resid 0.2800317 
## Run 5 stress 0.2242954 
## Run 6 stress 0.2059342 
## Run 7 stress 0.2216814 
## Run 8 stress 0.2321686 
## Run 9 stress 0.2031567 
## ... Procrustes: rmse 0.005703059  max resid 0.03236652 
## Run 10 stress 0.2054121 
## Run 11 stress 0.2030086 
## ... Procrustes: rmse 0.003652494  max resid 0.02897533 
## Run 12 stress 0.2275034 
## Run 13 stress 0.2096442 
## Run 14 stress 0.2053981 
## Run 15 stress 0.2274968 
## Run 16 stress 0.2309956 
## Run 17 stress 0.2093869 
## Run 18 stress 0.223591 
## Run 19 stress 0.2151978 
## Run 20 stress 0.2030163 
## ... Procrustes: rmse 0.01434893  max resid 0.08473071 
## *** Best solution was not repeated -- monoMDS stopping criteria:
##      7: no. of iterations >= maxit
##     13: stress ratio > sratmax
## Too few points to calculate an ellipse
## Too few points to calculate an ellipse

permanova(ps.rare.prop, 'Household + Breed.Group..1.', 'bray')
## PERMANOVA Model: bray~Household + Breed.Group..1.; Strata: None
## Permutation test for adonis under reduced model
## Terms added sequentially (first to last)
## Permutation: free
## Number of permutations: 10000
## 
## adonis2(formula = model, data = df, permutations = permutations, by = "term", parallel = core, na.action = na.omit, strata = strata)
##                 Df SumOfSqs      R2      F    Pr(>F)    
## Household       45  11.0103 0.69606 2.3094 9.999e-05 ***
## Breed.Group..1.  4   0.6758 0.04272 1.5946    0.0138 *  
## Residual        39   4.1319 0.26122                     
## Total           88  15.8180 1.00000                     
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
permdisp(ps.rare.prop, 'Breed.Group..1.', 'bray')
## PERMDISP Model: bray~Breed.Group..1.
## missing observations due to 'group' removed
## 
## Permutation test for homogeneity of multivariate dispersions
## Permutation: free
## Number of permutations: 10000
## 
## Response: Distances
##           Df  Sum Sq  Mean Sq      F N.Perm Pr(>F)
## Groups     6 0.08427 0.014045 0.8401  10000 0.5347
## Residuals 82 1.37093 0.016719

Weighted-Unifrac distance

plot_ord(ps.rare.prop, 'Breed.Group..1.','MDS','wunifrac')
## Too few points to calculate an ellipse
## Too few points to calculate an ellipse

plot_ord(ps.rare.prop, 'Breed.Group..1.','NMDS','wunifrac')
## Run 0 stress 0.1677058 
## Run 1 stress 0.1682005 
## ... Procrustes: rmse 0.03096808  max resid 0.1219196 
## Run 2 stress 0.1697945 
## Run 3 stress 0.1667604 
## ... New best solution
## ... Procrustes: rmse 0.06261475  max resid 0.1972576 
## Run 4 stress 0.1708367 
## Run 5 stress 0.1701862 
## Run 6 stress 0.1726216 
## Run 7 stress 0.1739057 
## Run 8 stress 0.1697232 
## Run 9 stress 0.1666855 
## ... New best solution
## ... Procrustes: rmse 0.06424002  max resid 0.2441771 
## Run 10 stress 0.1713096 
## Run 11 stress 0.1682001 
## Run 12 stress 0.176649 
## Run 13 stress 0.1696955 
## Run 14 stress 0.1690016 
## Run 15 stress 0.1727519 
## Run 16 stress 0.1768034 
## Run 17 stress 0.1732289 
## Run 18 stress 0.1656231 
## ... New best solution
## ... Procrustes: rmse 0.06462843  max resid 0.2446385 
## Run 19 stress 0.1680417 
## Run 20 stress 0.1708028 
## *** Best solution was not repeated -- monoMDS stopping criteria:
##     20: stress ratio > sratmax
## Too few points to calculate an ellipse
## Too few points to calculate an ellipse

permanova(ps.rare.prop, 'Household + Breed.Group..1.', 'wunifrac')
## PERMANOVA Model: wunifrac~Household + Breed.Group..1.; Strata: None
## Permutation test for adonis under reduced model
## Terms added sequentially (first to last)
## Permutation: free
## Number of permutations: 10000
## 
## adonis2(formula = model, data = df, permutations = permutations, by = "term", parallel = core, na.action = na.omit, strata = strata)
##                 Df SumOfSqs      R2      F    Pr(>F)    
## Household       45  1.72376 0.68194 2.2675 9.999e-05 ***
## Breed.Group..1.  4  0.14510 0.05740 2.1473    0.0044 ** 
## Residual        39  0.65885 0.26065                     
## Total           88  2.52771 1.00000                     
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
permdisp(ps.rare.prop, 'Breed.Group..1.', 'wunifrac')
## PERMDISP Model: wunifrac~Breed.Group..1.
## missing observations due to 'group' removed
## Warning in betadisper(dist.matrix, group = df[, group]): some squared distances
## are negative and changed to zero
## 
## Permutation test for homogeneity of multivariate dispersions
## Permutation: free
## Number of permutations: 10000
## 
## Response: Distances
##           Df  Sum Sq   Mean Sq      F N.Perm Pr(>F)
## Groups     6 0.00951 0.0015844 0.3352  10000 0.9183
## Residuals 82 0.38763 0.0047272

Drug Effect

Alpha Diversity

sam.drug <- sam %>% filter(Epileptic.or.Control == 'Epileptic')
ggplot(sam.drug, aes(x = Pheno.Y.N, y = Shannon)) + 
    geom_boxplot() + geom_jitter(height = 0, width = 0.25)

anova_test(Shannon~Pheno.Y.N, data = sam.drug, type = 1)
## ANOVA Table (type I tests)
## 
##      Effect DFn DFd     F     p p<.05   ges
## 1 Pheno.Y.N   1  47 1.024 0.317       0.021

Beta Diversity

Bray-Curtis distance

ps.drug <- ps.rare.prop %>% subset_samples(Epileptic.or.Control == 'Epileptic')
plot_ord(ps.drug, 'Pheno.Y.N','MDS','bray')

plot_ord(ps.drug, 'Pheno.Y.N','NMDS','bray')
## Run 0 stress 0.2019485 
## Run 1 stress 0.2068149 
## Run 2 stress 0.2113065 
## Run 3 stress 0.2012733 
## ... New best solution
## ... Procrustes: rmse 0.01510675  max resid 0.07095505 
## Run 4 stress 0.1992812 
## ... New best solution
## ... Procrustes: rmse 0.0633744  max resid 0.3343219 
## Run 5 stress 0.2084678 
## Run 6 stress 0.2188009 
## Run 7 stress 0.2081665 
## Run 8 stress 0.208545 
## Run 9 stress 0.1985675 
## ... New best solution
## ... Procrustes: rmse 0.06675735  max resid 0.3518324 
## Run 10 stress 0.2063789 
## Run 11 stress 0.2054378 
## Run 12 stress 0.2101436 
## Run 13 stress 0.2166458 
## Run 14 stress 0.2084301 
## Run 15 stress 0.2047598 
## Run 16 stress 0.2338534 
## Run 17 stress 0.2019475 
## Run 18 stress 0.2034402 
## Run 19 stress 0.2045289 
## Run 20 stress 0.2046103 
## *** Best solution was not repeated -- monoMDS stopping criteria:
##      3: no. of iterations >= maxit
##     17: stress ratio > sratmax

permanova(ps.drug, 'Pheno.Y.N', 'bray')
## PERMANOVA Model: bray~Pheno.Y.N; Strata: None
## Permutation test for adonis under reduced model
## Terms added sequentially (first to last)
## Permutation: free
## Number of permutations: 10000
## 
## adonis2(formula = model, data = df, permutations = permutations, by = "term", parallel = core, na.action = na.omit, strata = strata)
##           Df SumOfSqs     R2      F Pr(>F)
## Pheno.Y.N  1   0.2227 0.0261 1.2594 0.1736
## Residual  47   8.3114 0.9739              
## Total     48   8.5341 1.0000
permdisp(ps.drug, 'Pheno.Y.N', 'bray')
## PERMDISP Model: bray~Pheno.Y.N
## 
## Permutation test for homogeneity of multivariate dispersions
## Permutation: free
## Number of permutations: 10000
## 
## Response: Distances
##           Df  Sum Sq  Mean Sq      F N.Perm Pr(>F)
## Groups     1 0.01977 0.019774 1.5211  10000 0.2333
## Residuals 47 0.61098 0.013000

Weighted-Unifrac distance

plot_ord(ps.drug, 'Pheno.Y.N','MDS','wunifrac')

plot_ord(ps.drug, 'Pheno.Y.N','NMDS','wunifrac')
## Run 0 stress 0.1787197 
## Run 1 stress 0.1886127 
## Run 2 stress 0.1841812 
## Run 3 stress 0.1849113 
## Run 4 stress 0.1930409 
## Run 5 stress 0.1851422 
## Run 6 stress 0.1835741 
## Run 7 stress 0.1784344 
## ... New best solution
## ... Procrustes: rmse 0.04920273  max resid 0.1864762 
## Run 8 stress 0.1953738 
## Run 9 stress 0.1811354 
## Run 10 stress 0.1773422 
## ... New best solution
## ... Procrustes: rmse 0.02873592  max resid 0.1262116 
## Run 11 stress 0.1889761 
## Run 12 stress 0.1813755 
## Run 13 stress 0.1857661 
## Run 14 stress 0.1971116 
## Run 15 stress 0.1995912 
## Run 16 stress 0.1809059 
## Run 17 stress 0.1832221 
## Run 18 stress 0.1867063 
## Run 19 stress 0.2043014 
## Run 20 stress 0.1922891 
## *** Best solution was not repeated -- monoMDS stopping criteria:
##     20: stress ratio > sratmax

permanova(ps.drug, 'Pheno.Y.N', 'wunifrac')
## PERMANOVA Model: wunifrac~Pheno.Y.N; Strata: None
## Permutation test for adonis under reduced model
## Terms added sequentially (first to last)
## Permutation: free
## Number of permutations: 10000
## 
## adonis2(formula = model, data = df, permutations = permutations, by = "term", parallel = core, na.action = na.omit, strata = strata)
##           Df SumOfSqs      R2      F Pr(>F)
## Pheno.Y.N  1  0.02741 0.02232 1.0732 0.3549
## Residual  47  1.20053 0.97768              
## Total     48  1.22794 1.00000
permdisp(ps.drug, 'Pheno.Y.N', 'wunifrac')
## PERMDISP Model: wunifrac~Pheno.Y.N
## 
## Permutation test for homogeneity of multivariate dispersions
## Permutation: free
## Number of permutations: 10000
## 
## Response: Distances
##           Df   Sum Sq   Mean Sq      F N.Perm  Pr(>F)  
## Groups     1 0.010825 0.0108247 3.5692  10000 0.06439 .
## Residuals 47 0.142541 0.0030328                        
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

Sex Effect

prop.test(xtabs(~Household+Sex, data = sam))
## Warning in prop.test(xtabs(~Household + Sex, data = sam)): Chi-squared
## approximation may be incorrect
## 
##  49-sample test for equality of proportions without continuity
##  correction
## 
## data:  xtabs(~Household + Sex, data = sam)
## X-squared = 52.464, df = 48, p-value = 0.3051
## alternative hypothesis: two.sided
## sample estimates:
##  prop 1  prop 2  prop 3  prop 4  prop 5  prop 6  prop 7  prop 8  prop 9 prop 10 
##     0.5     0.5     0.0     1.0     0.0     1.0     1.0     0.5     1.0     1.0 
## prop 11 prop 12 prop 13 prop 14 prop 15 prop 16 prop 17 prop 18 prop 19 prop 20 
##     0.5     1.0     1.0     1.0     0.5     0.5     1.0     1.0     0.0     0.5 
## prop 21 prop 22 prop 23 prop 24 prop 25 prop 26 prop 27 prop 28 prop 29 prop 30 
##     0.0     0.5     1.0     0.5     0.5     0.0     0.0     0.0     0.5     0.5 
## prop 31 prop 32 prop 33 prop 34 prop 35 prop 36 prop 37 prop 38 prop 39 prop 40 
##     1.0     0.5     1.0     1.0     0.0     0.5     0.5     0.5     0.5     0.5 
## prop 41 prop 42 prop 43 prop 44 prop 45 prop 46 prop 47 prop 48 prop 49 
##     0.0     1.0     1.0     0.5     0.5     1.0     0.5     1.0     0.5

Alpha Diversity

ggplot(sam, aes(x = Sex, y = Shannon)) + 
    geom_boxplot() + geom_jitter(height = 0, width = 0.25)

anova_test(Shannon~Household+Sex, data = sam, type = 1)
## ANOVA Table (type I tests)
## 
##      Effect DFn DFd     F     p p<.05   ges
## 1 Household  48  48 1.634 0.046     * 0.620
## 2       Sex   1  48 2.085 0.155       0.042

Beta Diversity

Bray-Curtis distance

plot_ord(ps.rare.prop, 'Sex','MDS','bray')

plot_ord(ps.rare.prop, 'Sex','NMDS','bray')
## Run 0 stress 0.2089049 
## Run 1 stress 0.2030074 
## ... New best solution
## ... Procrustes: rmse 0.04311447  max resid 0.2831413 
## Run 2 stress 0.2241901 
## Run 3 stress 0.2040947 
## Run 4 stress 0.2042356 
## Run 5 stress 0.2046834 
## Run 6 stress 0.2366879 
## Run 7 stress 0.2046809 
## Run 8 stress 0.2098817 
## Run 9 stress 0.2034657 
## ... Procrustes: rmse 0.01101628  max resid 0.08021636 
## Run 10 stress 0.2042616 
## Run 11 stress 0.2049105 
## Run 12 stress 0.2118969 
## Run 13 stress 0.2146872 
## Run 14 stress 0.2041687 
## Run 15 stress 0.2206628 
## Run 16 stress 0.2037209 
## Run 17 stress 0.2098154 
## Run 18 stress 0.2135529 
## Run 19 stress 0.2088932 
## Run 20 stress 0.2237111 
## *** Best solution was not repeated -- monoMDS stopping criteria:
##      8: no. of iterations >= maxit
##     12: stress ratio > sratmax

permanova(ps.rare.prop, 'Household+Sex', 'bray')
## PERMANOVA Model: bray~Household+Sex; Strata: None
## Permutation test for adonis under reduced model
## Terms added sequentially (first to last)
## Permutation: free
## Number of permutations: 10000
## 
## adonis2(formula = model, data = df, permutations = permutations, by = "term", parallel = core, na.action = na.omit, strata = strata)
##           Df SumOfSqs      R2      F    Pr(>F)    
## Household 48  12.2248 0.68930 2.2419 9.999e-05 ***
## Sex        1   0.0575 0.00324 0.5062    0.9804    
## Residual  48   5.4529 0.30746                     
## Total     97  17.7352 1.00000                     
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
permdisp(ps.rare.prop, 'Sex', 'bray')
## PERMDISP Model: bray~Sex
## 
## Permutation test for homogeneity of multivariate dispersions
## Permutation: free
## Number of permutations: 10000
## 
## Response: Distances
##           Df  Sum Sq  Mean Sq      F N.Perm Pr(>F)
## Groups     1 0.01776 0.017760 1.2052  10000 0.2803
## Residuals 96 1.41464 0.014736

Weighted-Unifrac distance

plot_ord(ps.rare.prop, 'Sex','MDS','wunifrac')

plot_ord(ps.rare.prop, 'Sex','NMDS','wunifrac')
## Run 0 stress 0.1677058 
## Run 1 stress 0.1721876 
## Run 2 stress 0.1748774 
## Run 3 stress 0.1695608 
## Run 4 stress 0.1736527 
## Run 5 stress 0.1684567 
## Run 6 stress 0.173431 
## Run 7 stress 0.1798827 
## Run 8 stress 0.1686836 
## Run 9 stress 0.1732004 
## Run 10 stress 0.1773692 
## Run 11 stress 0.1658951 
## ... New best solution
## ... Procrustes: rmse 0.06261077  max resid 0.2351924 
## Run 12 stress 0.167848 
## Run 13 stress 0.1752635 
## Run 14 stress 0.1769925 
## Run 15 stress 0.1746874 
## Run 16 stress 0.1699411 
## Run 17 stress 0.1725597 
## Run 18 stress 0.1763439 
## Run 19 stress 0.1678521 
## Run 20 stress 0.1727219 
## *** Best solution was not repeated -- monoMDS stopping criteria:
##      2: no. of iterations >= maxit
##     18: stress ratio > sratmax

permanova(ps.rare.prop, 'Household+Sex', 'wunifrac')
## PERMANOVA Model: wunifrac~Household+Sex; Strata: None
## Permutation test for adonis under reduced model
## Terms added sequentially (first to last)
## Permutation: free
## Number of permutations: 10000
## 
## adonis2(formula = model, data = df, permutations = permutations, by = "term", parallel = core, na.action = na.omit, strata = strata)
##           Df SumOfSqs      R2      F    Pr(>F)    
## Household 48  1.90306 0.67767 2.1118 9.999e-05 ***
## Sex        1  0.00402 0.00143 0.2139    0.9968    
## Residual  48  0.90115 0.32090                     
## Total     97  2.80823 1.00000                     
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
permdisp(ps.rare.prop, 'Sex', 'wunifrac')
## PERMDISP Model: wunifrac~Sex
## 
## Permutation test for homogeneity of multivariate dispersions
## Permutation: free
## Number of permutations: 10000
## 
## Response: Distances
##           Df  Sum Sq   Mean Sq      F N.Perm Pr(>F)
## Groups     1 0.00158 0.0015789 0.3971  10000 0.5328
## Residuals 96 0.38166 0.0039757

Age Effect

Alpha Diversity

sam[which(is.na(sam$Age..months.)),'Household'] 
## [1] "9"  "24"
# remove households that have dog with unspecific age
sam.Age <- sam %>% filter(!(Household %in% c('9', '24')))
ggplot(sam.Age,) +
    geom_line(aes(x = as.numeric(Household), y = Age..months., group = Household)) + 
    geom_point(aes(x = as.numeric(Household), y = Age..months., group = Household, colour = Epileptic.or.Control)) +
    xlab('Household') + ylab('Age in month')

anova_test(Shannon~Household+Age..months., data = sam.Age, type = 1)
## ANOVA Table (type I tests)
## 
##         Effect DFn DFd     F     p p<.05      ges
## 1    Household  46  46 1.345 0.159       0.574000
## 2 Age..months.   1  46 0.010 0.921       0.000218

Beta Diversity

Bray-Curtis distance

ps.age <- ps.rare.prop %>% subset_samples(!(Household %in% c('9', '24')))
permanova(ps.age, 'Age..months.', 'bray', strata = 'Household')
## PERMANOVA Model: bray~Age..months.; Strata: Household
## Permutation test for adonis under reduced model
## Terms added sequentially (first to last)
## Blocks:  strata 
## Permutation: free
## Number of permutations: 10000
## 
## adonis2(formula = model, data = df, permutations = permutations, by = "term", parallel = core, na.action = na.omit, strata = strata)
##              Df SumOfSqs      R2      F Pr(>F)
## Age..months.  1    0.271 0.01583 1.4795 0.3021
## Residual     92   16.849 0.98417              
## Total        93   17.120 1.00000

Weighted-Unifrac distance

permanova(ps.age, 'Age..months.', 'wunifrac')
## PERMANOVA Model: wunifrac~Age..months.; Strata: None
## Permutation test for adonis under reduced model
## Terms added sequentially (first to last)
## Permutation: free
## Number of permutations: 10000
## 
## adonis2(formula = model, data = df, permutations = permutations, by = "term", parallel = core, na.action = na.omit, strata = strata)
##              Df SumOfSqs      R2      F Pr(>F)
## Age..months.  1  0.03898 0.01437 1.3414 0.1918
## Residual     92  2.67326 0.98563              
## Total        93  2.71224 1.00000
sessioninfo::session_info()
## ─ Session info ───────────────────────────────────────────────────────────────
##  setting  value
##  version  R version 4.4.1 (2024-06-14)
##  os       macOS 15.2
##  system   aarch64, darwin20
##  ui       X11
##  language (EN)
##  collate  en_US.UTF-8
##  ctype    en_US.UTF-8
##  tz       America/New_York
##  date     2025-01-01
##  pandoc   3.5 @ /Users/yixuanyang/miniforge3/bin/ (via rmarkdown)
## 
## ─ Packages ───────────────────────────────────────────────────────────────────
##  package          * version  date (UTC) lib source
##  abind              1.4-8    2024-09-12 [1] CRAN (R 4.4.1)
##  ade4               1.7-22   2023-02-06 [1] CRAN (R 4.4.0)
##  ape                5.8-1    2024-12-16 [1] CRAN (R 4.4.1)
##  backports          1.5.0    2024-05-23 [1] CRAN (R 4.4.0)
##  Biobase            2.66.0   2024-10-29 [1] Bioconductor 3.20 (R 4.4.1)
##  BiocGenerics       0.52.0   2024-10-29 [1] Bioconductor 3.20 (R 4.4.1)
##  biomformat         1.34.0   2024-10-29 [1] Bioconductor 3.20 (R 4.4.1)
##  Biostrings         2.74.0   2024-10-29 [1] Bioconductor 3.20 (R 4.4.1)
##  broom              1.0.7    2024-09-26 [1] CRAN (R 4.4.1)
##  bslib              0.8.0    2024-07-29 [1] CRAN (R 4.4.0)
##  cachem             1.1.0    2024-05-16 [1] CRAN (R 4.4.0)
##  car                3.1-3    2024-09-27 [1] CRAN (R 4.4.1)
##  carData            3.0-5    2022-01-06 [1] CRAN (R 4.4.0)
##  cli                3.6.3    2024-06-21 [1] CRAN (R 4.4.0)
##  cluster            2.1.8    2024-12-11 [1] CRAN (R 4.4.1)
##  codetools          0.2-20   2024-03-31 [1] CRAN (R 4.4.1)
##  colorspace         2.1-1    2024-07-26 [1] CRAN (R 4.4.0)
##  crayon             1.5.3    2024-06-20 [1] CRAN (R 4.4.0)
##  data.table         1.16.4   2024-12-06 [1] CRAN (R 4.4.1)
##  digest             0.6.37   2024-08-19 [1] CRAN (R 4.4.1)
##  dplyr            * 1.1.4    2023-11-17 [1] CRAN (R 4.4.0)
##  evaluate           1.0.1    2024-10-10 [1] CRAN (R 4.4.1)
##  farver             2.1.2    2024-05-13 [1] CRAN (R 4.4.0)
##  fastmap            1.2.0    2024-05-15 [1] CRAN (R 4.4.0)
##  forcats          * 1.0.0    2023-01-29 [1] CRAN (R 4.4.0)
##  foreach            1.5.2    2022-02-02 [1] CRAN (R 4.4.0)
##  Formula            1.2-5    2023-02-24 [1] CRAN (R 4.4.0)
##  generics           0.1.3    2022-07-05 [1] CRAN (R 4.4.0)
##  GenomeInfoDb       1.42.0   2024-10-29 [1] Bioconductor 3.20 (R 4.4.1)
##  GenomeInfoDbData   1.2.13   2024-10-05 [1] Bioconductor
##  ggplot2          * 3.5.1    2024-04-23 [1] CRAN (R 4.4.0)
##  glue               1.8.0    2024-09-30 [1] CRAN (R 4.4.1)
##  gtable             0.3.6    2024-10-25 [1] CRAN (R 4.4.1)
##  here             * 1.0.1    2020-12-13 [1] CRAN (R 4.4.0)
##  hms                1.1.3    2023-03-21 [1] CRAN (R 4.4.0)
##  htmltools          0.5.8.1  2024-04-04 [1] CRAN (R 4.4.0)
##  httr               1.4.7    2023-08-15 [1] CRAN (R 4.4.0)
##  igraph             2.1.2    2024-12-07 [1] CRAN (R 4.4.1)
##  IRanges            2.40.0   2024-10-29 [1] Bioconductor 3.20 (R 4.4.1)
##  iterators          1.0.14   2022-02-05 [1] CRAN (R 4.4.0)
##  jquerylib          0.1.4    2021-04-26 [1] CRAN (R 4.4.0)
##  jsonlite           1.8.9    2024-09-20 [1] CRAN (R 4.4.1)
##  knitr              1.49     2024-11-08 [1] CRAN (R 4.4.1)
##  labeling           0.4.3    2023-08-29 [1] CRAN (R 4.4.0)
##  lattice          * 0.22-6   2024-03-20 [1] CRAN (R 4.4.1)
##  lifecycle          1.0.4    2023-11-07 [1] CRAN (R 4.4.0)
##  lubridate        * 1.9.4    2024-12-08 [1] CRAN (R 4.4.1)
##  magrittr           2.0.3    2022-03-30 [1] CRAN (R 4.4.0)
##  MASS               7.3-61   2024-06-13 [1] CRAN (R 4.4.0)
##  Matrix             1.7-1    2024-10-18 [1] CRAN (R 4.4.1)
##  mgcv               1.9-1    2023-12-21 [1] CRAN (R 4.4.1)
##  multtest           2.62.0   2024-10-29 [1] Bioconductor 3.20 (R 4.4.1)
##  munsell            0.5.1    2024-04-01 [1] CRAN (R 4.4.0)
##  nlme               3.1-166  2024-08-14 [1] CRAN (R 4.4.0)
##  permute          * 0.9-7    2022-01-27 [1] CRAN (R 4.4.0)
##  phyloseq         * 1.50.0   2024-10-29 [1] Bioconductor 3.20 (R 4.4.1)
##  pillar             1.10.0   2024-12-17 [1] CRAN (R 4.4.1)
##  pkgconfig          2.0.3    2019-09-22 [1] CRAN (R 4.4.0)
##  plyr               1.8.9    2023-10-02 [1] CRAN (R 4.4.0)
##  purrr            * 1.0.2    2023-08-10 [1] CRAN (R 4.4.0)
##  R6                 2.5.1    2021-08-19 [1] CRAN (R 4.4.0)
##  Rcpp               1.0.13-1 2024-11-02 [1] CRAN (R 4.4.1)
##  readr            * 2.1.5    2024-01-10 [1] CRAN (R 4.4.0)
##  reshape2           1.4.4    2020-04-09 [1] CRAN (R 4.4.0)
##  rhdf5              2.49.0   2024-05-04 [1] Bioconductor 3.20 (R 4.4.0)
##  rhdf5filters       1.17.0   2024-05-04 [1] Bioconductor 3.20 (R 4.4.0)
##  Rhdf5lib           1.27.0   2024-05-04 [1] Bioconductor 3.20 (R 4.4.0)
##  rlang              1.1.4    2024-06-04 [1] CRAN (R 4.4.0)
##  rmarkdown          2.29     2024-11-04 [1] CRAN (R 4.4.1)
##  rprojroot          2.0.4    2023-11-05 [1] CRAN (R 4.4.0)
##  rstatix          * 0.7.2    2023-02-01 [1] CRAN (R 4.4.0)
##  rstudioapi         0.17.1   2024-10-22 [1] CRAN (R 4.4.1)
##  S4Vectors          0.44.0   2024-10-29 [1] Bioconductor 3.20 (R 4.4.1)
##  sass               0.4.9    2024-03-15 [1] CRAN (R 4.4.0)
##  scales             1.3.0    2023-11-28 [1] CRAN (R 4.4.0)
##  sessioninfo        1.2.2    2021-12-06 [1] CRAN (R 4.4.0)
##  stringi            1.8.4    2024-05-06 [1] CRAN (R 4.4.0)
##  stringr          * 1.5.1    2023-11-14 [1] CRAN (R 4.4.0)
##  survival           3.8-3    2024-12-17 [1] CRAN (R 4.4.1)
##  tibble           * 3.2.1    2023-03-20 [1] CRAN (R 4.4.0)
##  tidyr            * 1.3.1    2024-01-24 [1] CRAN (R 4.4.0)
##  tidyselect         1.2.1    2024-03-11 [1] CRAN (R 4.4.0)
##  tidyverse        * 2.0.0    2023-02-22 [1] CRAN (R 4.4.0)
##  timechange         0.3.0    2024-01-18 [1] CRAN (R 4.4.0)
##  tzdb               0.4.0    2023-05-12 [1] CRAN (R 4.4.0)
##  UCSC.utils         1.2.0    2024-10-29 [1] Bioconductor 3.20 (R 4.4.1)
##  vctrs              0.6.5    2023-12-01 [1] CRAN (R 4.4.0)
##  vegan            * 2.6-8    2024-08-28 [1] CRAN (R 4.4.1)
##  withr              3.0.2    2024-10-28 [1] CRAN (R 4.4.1)
##  xfun               0.49     2024-10-31 [1] CRAN (R 4.4.1)
##  XVector            0.46.0   2024-10-29 [1] Bioconductor 3.20 (R 4.4.1)
##  yaml               2.3.10   2024-07-26 [1] CRAN (R 4.4.0)
##  zlibbioc           1.52.0   2024-10-29 [1] Bioconductor 3.20 (R 4.4.1)
## 
##  [1] /Library/Frameworks/R.framework/Versions/4.4-arm64/Resources/library
## 
## ──────────────────────────────────────────────────────────────────────────────